class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int buy,sell,ans=0;
        prices.insert(prices.begin(),INT_MAX);
        prices.push_back(-INT_MAX);
        for(int i=1;i<prices.size()-1;i++){
            if(prices[i-1]>prices[i]&&prices[i]<=prices[i+1]){buy=prices[i];}
            if(prices[i-1]<=prices[i]&&prices[i]>prices[i+1]){
                sell=prices[i];
                ans=ans+sell-buy;
            }
        }
        return ans;
    }
};